package com.itany.hotel.action;

import com.itany.hotel.entity.Member;
import com.itany.hotel.exception.DuplicateUsernameException;
import com.itany.hotel.exception.UserNotFoundException;
import com.itany.hotel.service.MemberService;
import com.itany.hotel.util.ActionFormUtil;
import framework.bean.factory.ObjectFactory;
import framework.mvc.annotation.Action;
import framework.mvc.annotation.Controller;
import framework.mvc.annotation.Result;
import framework.mvc.enu.ResultType;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@Action(path = "/member")
public class MemberAction {


    @Action(path = "/showRegist1", forward = "/WEB-INF/page/register-1.jsp")
    public void showRegist1() {
    }

    @Action(path = "/showRegist2", forward = "/WEB-INF/page/register-2.jsp")
    public void showRegist2() {
    }

    @Action(path = "/showLogin", forward = "/WEB-INF/page/login.jsp")
    public void showLogin() {
    }


    @Action(path = "/regist", results = {
            @Result(name = "success", path = "/member/showRegist2.action", type = ResultType.REDIRECT),
            @Result(name = "duplicateUsername", path = "/WEB-INF/page/register-1.jsp")
    })
    public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException {


        Member member = ActionFormUtil.parserRequest(request, Member.class);

        MemberService memberService = (MemberService) ObjectFactory.getObject("memberService");

        try {

            memberService.regist(member);

            return "success";
        } catch (DuplicateUsernameException e) {

            request.setAttribute("message",e.getMessage());

            return "duplicateUsername";
        }

    }


    @Action(path = "/login", results = {
            @Result(name = "success", path = "/subscription/check/findUnacknowledged.action", type = ResultType.REDIRECT),
            @Result(name = "userNotFound", path = "/WEB-INF/page/login.jsp")
    })
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException {


        String username=request.getParameter("username");
        String password=request.getParameter("password");

        MemberService memberService = (MemberService) ObjectFactory.getObject("memberService");


        try {
            Member member=memberService.login(username,password);

            request.getSession().setAttribute("member",member);

            return "success";

        } catch (UserNotFoundException e) {

            request.setAttribute("message",e.getMessage());

            return "userNotFound";
        }

    }

    @Action(path = "/check/logout", results = {
            @Result(name = "success", path = "/showIndex.action", type = ResultType.REDIRECT)
    })
    public String logout(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        request.getSession().invalidate();
        return "success";
    }
}